package com.hxzy.tzxm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.system.domain.dto.ValueExistsDTO;
import com.hxzy.tzxm.domain.CmsCategory;
import com.hxzy.tzxm.mapper.CmsCategoryMapper;
import com.hxzy.tzxm.service.CmsCategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 栏目管理
 */
@Service
public class CmsCategoryServiceImpl extends ServiceImpl<CmsCategoryMapper, CmsCategory> implements CmsCategoryService {


    @Override
    public List<CmsCategory> search() {
        return this.baseMapper.search();
    }

    @Override
    public List<CmsCategory> buildTree(List<CmsCategory> arrList) {
        List<CmsCategory> treeList=new ArrayList<>();

        arrList.stream().filter(p->p.getCategoryId().intValue()==0).forEach(p->{
            // 递归遍历子节点
            recursionFn(arrList,p);

            treeList.add(p);
        });

        return treeList;
    }

    @Override
    public int checkPinYingUnique(ValueExistsDTO pinYingExistsDTO) {
        return this.baseMapper.checkPinYingUnique(pinYingExistsDTO);
    }

    @Override
    public int delete(Long id) {

        return this.baseMapper.updateDelFlag(id);
    }

    /**
     * 递归遍历子节点
     * @param arrList
     * @param root
     */
    private void recursionFn(List<CmsCategory> arrList, CmsCategory root){
        List<CmsCategory> childrenList=new ArrayList<>();

        arrList.stream().filter(p->p.getCategoryId().intValue()==root.getId().intValue()).forEach(p->{
            // 递归遍历子节点
            recursionFn(arrList,p);

            childrenList.add(p);
        });

        if(childrenList.size()>0){
            root.setChildren(childrenList);
        }
    }
}
